// #define print(...)
#define r2i(r) dict[(r)-'C']
class Solution {
public:
    int romanToInt(string s) {
        vector<int> dict('X'-'C'+1, 0);
        r2i('I') = 1;
        r2i('V') = 5;
        r2i('X') = 10;
        r2i('L') = 50;
        r2i('C') = 100;
        r2i('D') = 500;
        r2i('M') = 1000;
//        print("dict=", dict, "\n")
        if(s.size() == 1) return r2i(s[0]);
        int res = 0;
        auto it=s.begin();
        while(++it != s.end()) {
            res += r2i(*(it-1)) < r2i(*it) ? -r2i(*(it-1)) : r2i(*(it-1));
        };
        return res + r2i(*(it-1));
    }
    
    string intToRoman(int num) {
        tuple<int, string> dict[] = {
            {1000, "M"},
            {900,  "CM"},
            {500,  "D"},
            {400,  "CD"},
            {100,  "C"},
            {90,   "XC"},
            {50,   "L"},
            {40,   "XL"},
            {10,   "X"},
            {9,    "IX"},
            {5,    "V"},
            {4,    "IV"},
            {1,    "I"},
        };
        string rom;
        for(auto&[v,r]:dict){
            while(num >= v){
                rom.append(r);
                num -= v;
            }
        }
        return rom;
    }
};
